var express = require('express');
var router = express.Router();
//加载数据库
var connection = require('../db/sql.js');
//加载腾讯云短信
//var QcloudSms = require("qcloudsms_js");
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
//查询航班信息的接口，返回全部航班信息
router.get('/api/selectFlight', function (req, res, next) {
  let aplace = req.query.aplace;
  let bplace = req.query.bplace;
  let date = req.query.date;
  connection.query('select * from `flight` where `aplace`="' + aplace + '" and `bplace`="' + bplace + '" and `date`="' + date + '"', function (error, results) {
    if (error) throw error;
    //res.json和res.send都行
    res.json({
      code: 0,
      data: results
    })
  })
})
//新增旅客订单
router.post('/api/addTravelOrder', function (req, res, next) {
  let body = req.body;
  let [tel, name, idCard, unit, aplace, bplace, atime, btime, date, flight, elapsedTime, price] = [
    body.tel,
    body.name,
    body.idCard,
    body.unit,
    body.aplace,
    body.bplace,
    body.atime,
    body.btime,
    body.date,
    body.flight,
    body.elapsedTime,
    body.price,
  ];
  connection.query(`insert into traveller_order (tel,name,idCard,unit,aplace,bplace,atime,btime,date,status,flight,elapsedTime,price) values ("${tel}","${name}","${idCard}","${unit}","${aplace}","${bplace}","${atime}","${btime}","${date}","0","${flight}","${elapsedTime}","${price}")`, function (err, result) {
    if (err) throw err;
    res.send({
      data: {
        code: 200,
        success: true,
        msg: '收货地址添加成功'
      }
    })
  })
})
//查询航班信息的接口，返回全部航班信息
router.get('/api/selectOrder', function (req, res, next) {
  let tel = req.query.tel;
  connection.query('select * from `traveller_order` where `tel`="' + tel + '"', function (error, results) {
    if (error) throw error;
    //res.json和res.send都行
    res.json({
      code: 0,
      data: results
    })
  })
})
//修改旅客订单状态
router.post('/api/updateStatus', function (req, res, next) {
  let body = req.body;
  let [orderId,newStatus] = [
    body.orderId,
    body.newStatus,
  ];
  connection.query('update traveller_order set status="' + newStatus + '" where autoId="' + orderId + '"', function (err, result) {
    if (err) throw err;
    res.send({
      data: {
        code: 200,
        success: true,
        msg: '修改订单状态成功'
      }
    })
  })
})
module.exports = router;
